/**
 * Created by Administrator on 2017/4/25 0025.
 */
var container;
var camera, scene, renderer;
var beta=0;
var gamma=0;
var controls;
var kale;
var bool_render=false;
var bool_loading=true;
function threeInit()
{
    container = document.getElementById( 'div_3d' );

    camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 3000 );
    camera.aspect =window.innerWidth / window.innerHeight;
    camera.position.z = 350;



    scene = new THREE.Scene();

    var ambientLight = new THREE.AmbientLight(0xffffff, 1);
    scene.add(ambientLight);

    kale=Kaleidoscope.create(new THREE.TextureLoader().load('0.jpg'),1100,1100);
    scene.add( kale.mesh );

    renderer = new THREE.WebGLRenderer();
    renderer.setPixelRatio( window.devicePixelRatio );
    container.appendChild( renderer.domElement );
    onWindowResize();

    window.addEventListener( 'resize', onWindowResize, false );
    window.addEventListener('deviceorientation', function(e){
        beta=(e.beta)/2000;
        gamma=(e.gamma-25)/2200;
    });
    bool_render=true;
    animate();
}

function onWindowResize( event ) {

    camera.aspect =window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize( window.innerWidth, window.innerHeight );
}

function animate() {
    if(bool_render)
    {
        requestAnimationFrame( animate );
        render();
    }
}

function render() {
    if(bool_loading)
    {
        kale.kaleShader.uniforms.resolution.value.x = 1;
        kale.kaleShader.uniforms.resolution.value.y = 1;
        if(0.01+beta>0.1)
        {
            kale.kaleShader.uniforms.time.value+=0.1
        }
        else if(0.01+beta<-0.1)
        {
            kale.kaleShader.uniforms.time.value+=-0.1
        }
        else
        {
            kale.kaleShader.uniforms.time.value += (0.005+gamma);
        }
        kale.kaleShader.uniforms.angle.value+=beta;
    }
    if(bool_render)
    {
        renderer.render( scene, camera );
    }

}

function threeOut()
{
    bool_render=false;
}
